import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

import distribuida.main.ServidorWS;
import distribuida.main.ServidorWS_Service;

public class ClientImpl {
    
    public static ServidorWS_Service service = null;
    public static ServidorWS servico = null;
    static int numJogadas = 0;
    
    public static void main (String[] args) throws Exception {
        try {
            service = new ServidorWS_Service();
            servico = (ServidorWS) service.getServidorWSPort();
            System.out.println(servico.hello("Super Trunfo - Client"));
            System.out.println("Executar via linha de comando(1) ou arquivo(2)?");
            int leitura = new Scanner(System.in).nextInt();
            if(leitura == 1){
                while(true){
                    leituraTeclado();
                }
            }else{
                leituraArquivo();
            }

        } catch (Exception e) {
            System.out.println ("WS Failed! - SuperTrunfo failed.");
            e.printStackTrace();
        }
        
    }
    
    public static void leituraTeclado() {
        Scanner leitura = new Scanner(System.in);
        System.out.println("Informe a operação: ");
        String valorString = leitura.next();
        process(valorString);
        leitura.close();
    }
    
    public static void leituraArquivo() {
        try {
            String arq = "C:\\Users\\Notebook\\Downloads\\teste-001.in";
            BufferedReader in = new BufferedReader(new FileReader(arq));
            String str;
            while (in.ready()) {
                str = in.readLine();
                process(str);
            }
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public static void process(String linha) {
        String[] part = linha.split("\t");
        if (part[0].equals(""))
            return;
        int resp = -10;
        if (part[0].equals("0")) {
            String[] part2 = part[1].split(":");
            resp = servico.preRegistro(part2[0], Integer.parseInt(part2[1].trim()));
            System.out.println(resp);
        } else if (part[0].equals("1")) {
            resp = servico.registro(part[1].trim());
            System.out.println(resp);
        } else if (part[0].equals("2")) {
            String[] part2 = part[1].split(":");
            int opcao = Integer.parseInt(part2[1].trim());
            
            switch(opcao){
                case 1 : opcao = 4; break;
                case 2 : opcao = 5; break;
                case 3 : opcao = 6; break;
                case 4 : opcao = 7; break;
            }
            servico.definirCaracteristica(Integer.parseInt(part2[0]), opcao);
            servico.executarRodada(Integer.parseInt(part2[0]));
            System.out.println(0);
        } else if (part[0].equals("3")) {
            resp = servico.pontuacao(Integer.parseInt(part[1].trim()));
            System.out.println(resp);
        } else if (part[0].equals("4")) {
            resp = servico.remover(Integer.parseInt(part[1].trim()));
            System.out.println(resp);
        } else {
            numJogadas = Integer.parseInt(part[0]);
        }
    }
    
}
